package yxy.game.pm2.active.module.subordinateclass;

import org.joda.time.DateTime;
import org.redisson.api.RList;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.Collection;
import java.util.List;

public class Act_夺宝大奖纪录 extends BaseGameRedisBean {

	public int areaid;
	public int type;
	public String name;
	public Currency cur;
	public long time;
	
	public Act_夺宝大奖纪录(){
		
	}
	
	public static Act_夺宝大奖纪录 create(int areaid, int type, String name, Currency cur) {
		Act_夺宝大奖纪录 am = new Act_夺宝大奖纪录();
		am.setAreaid(areaid);
		am.setCur(cur);
		am.setName(name);
		am.setType(type);
		am.setTime(DateTime.now().getMillis());
		return am;
	}

	public int getAreaid() {
		return areaid;
	}
	public void setAreaid(int areaid) {
		this.areaid = areaid;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Currency getCur() {
		return cur;
	}
	public void setCur(Currency cur) {
		this.cur = cur;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}

	public String key() {
		return String.format("AREA:%s:TURNTABLE:TYPE:%s", getAreaid(), getType());
	}
	
	public static String key(int areaid, int type) {
		return String.format("AREA:%s:TURNTABLE:TYPE:%s", areaid, type);
	}
	
	public RList<Act_夺宝大奖纪录> touch() {
		return redis().client().getList(key());
	}
	
	/** 历史记录保存数量 -1表示不删除历史记录 */
	public int historySize() {
		return 20;
	}

	public void add() {
		try {
			if (tryLock()) {
				RList<Act_夺宝大奖纪录> list = touch();
				list.add(this);

				if (list.size() > historySize() && historySize() > 0) {
					list.remove(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}
	
	public static List<Act_夺宝大奖纪录> all(int areaid, int type){
		return redis().client().getList(key(areaid, type));
	}
	
	public Collection<Act_夺宝大奖纪录> all() {
		return touch().readAll();
	}
	
	@Override
	public String lockKey() {
		return String.format("lock:%s", key());
	}

}
